define(function(require) {
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	var fn = require("$UI/ls_common/fn");
	var Model = function() {
		this.callParent();
		this.FID = "";
	};
	Model.prototype.ProductApi = function() {
		console.log("ProductApi()");
		var self = this;
		var api_url = window.app.base_url + window.app.ProductApi;
		var data = {};
		data.username = window.app.username;
		data.password = window.app.password;
		console.log(window.app.ProductApi_ret);
		if ($.isEmptyObject(window.app.ProductApi_ret)) {
			console.log("尚未获取产品列表");
			$.ajax({
				type : "POST",
				url : require.toUrl(api_url),
				data : {
					'data' : JSON.stringify(data)
				},
				dataType : 'json',
				success : function(res) {
					console.log(res);
					window.app.ProductApi_ret = res;
					self.comp("ProductData").clear();
					self.comp("ProductData").loadData(res);
					console.log(self.comp("ProductData"));
					return res;
				},
				error : function(e) {
					return {};
				}
			});
		} else {
			console.log("已经获取过产品列表");
			console.log(window.app.ProductApi_ret);
			this.comp("ProductData").clear();
			this.comp("ProductData").loadData(window.app.ProductApi_ret);
		}
		this.comp("ProductData").each(function(param) {
			param.row.val("PrdName", param.row.val("PrdName") + "(" + param.row.val("PrdSpf") + ")");
		});
	};
	Model.prototype.CreateFranchiserInfo_Success = function(data, call_status) {
		console.log("CreateFranchiserInfo_Success()");
		console.log(data);
		console.log(call_status);
		this.comp("FaHuoLieBiaoData").setValue("SInfoID", data.SInfoID);   
		fn.cat_x5_data(this.comp("FaHuoLieBiaoData"));
		var List = this.comp("ProductData").find([ "PrdNo" ], [ this.comp("FaHuoLieBiaoData").val("PrdNo") ]);
		if (List.length > 0) {
			console.log("锁定........");
			this.comp("ProductData").setValue("isSelected", true, List[0]);
		}
		fn.cat_x5_data(this.comp("ProductData"));
		if (call_status == "From_AddCreateFranchiserInfo_ButtonClick") {
		
			this.comp("FaHuoLieBiaoData").newData({
				"index" : -1,
				"defaultValues" : [ {
					"fID" : justep.UUID.createUUID(),
					"SInfoID" : "",
					"PrdNo" : "",
					"PrdCount" : 0,
					"ScanCount" : 0,
				} ]
			});
			fn.cat_x5_data(this.comp("FaHuoLieBiaoData"));
		}
		if (call_status == "From_QRScan_ButtonClick") {
			console.log("From_QRScan_ButtonClick()");
			// 切换的扫码界面
			var params = {};
			params.SID = this.SID;
			params.SInfoID = this.comp("FaHuoLieBiaoData").getValue("SInfoID");
			params.PrdCount = this.comp("FaHuoLieBiaoData").val("PrdCount");
			this.getParent().comp("ScanCodeSell_Window").load("$UI/ls_shaomiao/ScanCodeSell.w", params);
			this.getParent().comp("Main_Contents").to("ScanCodeSell_Content");
		}
		if (call_status == "TitleBarSave_ButtonClick") {
			// 跳转到发货列表页面
			justep.Util.hint("保存成功");
			this.getParent().comp("Main_Contents").to("ShippingRecord_Content");
			return;
		}
	};
	Model.prototype.CreateFranchiserInfo_err = function(err, call_status) {
		console.log("CreateFranchiserInfo_err()");
		console.log(err);
		console.log(call_status);
	};
	Model.prototype.CreateFranchiserInfo = function(call_status) { // 添加一条发货明细
		console.log("CreateFranchiserInfo()");

		var data = {};
		data.username = window.app.username;
		data.password = window.app.password;
		data.SID = this.SID;
		data.SN = this.SN;
		data.PrdNo = this.comp("FaHuoLieBiaoData").val("PrdNo");
		data.PrdCount = this.comp("FaHuoLieBiaoData").val("PrdCount");
		data.CtmType = window.app.CtmType;

		console.log(data);
		if (this.comp("FaHuoLieBiaoData").val("SInfoID") !== "") {
			this.comp("FaHuoLieBiaoData").newData({
				"index" : 0,
				"defaultValues" : [ {
					"fID" : justep.UUID.createUUID(),
					"SInfoID" : "",
					"PrdNo" : "",
					"PrdCount" : 0,
					"ScanCount" : 0
				} ]
			});
			return;
		}
		var self = this;
		var api_url = window.app.base_url + window.app.SellInfoInsertApi;
		$.ajax({
			type : "POST",
			url : require.toUrl(api_url),
			data : {
				'data' : JSON.stringify(data)
			},
			dataType : 'json',
			success : function(res) {
				console.log(res);
				// SInfoID被取得到了
				console.log(res[0].SInfoID);
				self.CreateFranchiserInfo_Success(res[0], call_status);
				return;
			},
			error : function(err) {
				self.CreateFranchiserInfo_err(err, call_status);
				return;
			}
		});
	};
	Model.prototype.CurrentData_Check = function(row) { // 检测当前行的数据是否填写完整
		console.log("CurrentData_Check()");
		if (row.val("PrdNo") === undefined || row.val("PrdNo").length < 1) {
			justep.Util.hint("请选择产品分类");
			return false;
		}
		if (row.val("PrdCount") === undefined || row.val("PrdCount") < 1) {
			justep.Util.hint("请填写发货数量");
			return false;
		}
		return true;
	};
	Model.prototype.AddCreateFranchiserInfo_ButtonClick = function(event) {
		console.log("AddCreateFranchiserInfo_ButtonClick()");
		if (this.CurrentData_Check(this.comp("FaHuoLieBiaoData").getLastRow()) === false) { // 用户没有填写必要的信息
			return;
		}
		this.CreateFranchiserInfo("From_AddCreateFranchiserInfo_ButtonClick");
	};
	Model.prototype.QRScan_ButtonClick = function(event) {
		console.log("QRScan_ButtonClick()");
		this.FaHuoLieBiao_Current_row = event.bindingContext.$object;

		if (this.CurrentData_Check(this.FaHuoLieBiao_Current_row) === false) { // 用户没有填写必要的信息
			return;
		}
		this.comp("FaHuoLieBiaoData").to(this.FaHuoLieBiao_Current_row);
		if (this.comp("FaHuoLieBiaoData").getValue("SInfoID") === "") {
			// 如果这行的订单还没创建进入创建阶段
			console.log(this.comp("FaHuoLieBiaoData"));
			this.CreateFranchiserInfo("From_QRScan_ButtonClick");
		} else {
			// 如果已经创建，进入编辑阶段
			// 切换的扫码界面
			var params = {};
			params.SID = this.SID;
			params.SInfoID = this.comp("FaHuoLieBiaoData").getValue("SInfoID");
			params.PrdCount = this.comp("FaHuoLieBiaoData").val("PrdCount");
			this.getParent().comp("ScanCodeSell_Window").load("$UI/ls_shaomiao/ScanCodeSell.w", params);
			this.getParent().comp("Main_Contents").to("ScanCodeSell_Content");
		}
	};

	Model.prototype.input1Focus = function(event) {
		console.log("input1Focus");
		console.log(event);
		event.source.value = "";
	};

	Model.prototype.TitleBarSave_ButtonClick = function(event) {
		// 提示正在保存
		// this.comp("popOver").show();
		console.log("TitleBarSave_ButtonClick");
		var self = this;
		this.comp("FaHuoLieBiaoData").eachAll(function(param) {
			// console.log(param.row.val("SInfoID"));
			if (param.row.val("SInfoID") === "") {
				if (self.comp("FaHuoLieBiaoData").getLastRow().val("PrdNo").length > 0 && self.comp("FaHuoLieBiaoData").getLastRow().val("PrdCount") !== 0) {
					self.CreateFranchiserInfo("TitleBarSave_ButtonClick");
				} else {
					justep.Util.hint("没有正确填写发货信息，无法保存");
					return;
				}
			}

		});
		this.getParent().comp("ShippingRecord_Window").load("$UI/ls_shaomiao/Log.w");
		this.getParent().comp("Main_Contents").to("ShippingRecord_Content");
		// 提交任务
	};

	Model.prototype.modelParamsReceive = function(event) {
		console.log("modelParmasRecive()");
		$(this.getElementByXid("SendButton")).hide();
		console.log(event);
		this.comp("FaHuoLieBiaoData").clear();
		// 获取产品数据
		this.ProductApi();

		if (event.params === undefined || event.params.SID === undefined) {
			justep.Util.hint("主订单号获取失败，出错了。");
			this.getParent().comp("Main_Contents").to("ScanningDelivery_Content");
			return;
		} else {
			this.SID = event.params.SID;
			this.SN = event.params.SN;
		}

		if (event.params.products === undefined) {
			console.log("从创建主单号入口进来的");
			// 创建一条空记录
			this.comp("FaHuoLieBiaoData").newData({
				"index" : 0,
				"defaultValues" : [ {
					"fID" : justep.UUID.createUUID(),
					"SInfoID" : "",
					"PrdNo" : "",
					"SN" : "",
					"PrdCount" : 0,
					"ScanCount" : 0
				} ]
			});
			return;
		} else {
			console.log("从发货记录页面过来的"); // 直销 此功能暂时未支持
			var obj_arr = fn.String_to_ObjectArray(event.params.products);
			var self = this;
			obj_arr.forEach(function(e) {
				console.log(e);
				self.comp("FaHuoLieBiaoData").newData({
					"index" : 0,
					"defaultValues" : [ {
						"fID" : justep.UUID.createUUID(),
						"SInfoID" : e.ID,
						"PrdNo" : e.PrdNo,
						"PrdCount" : e.PrdCount,
						"SN" : e.SN,
						"ScanCount" : 0
					} ]
				});
			});

			// 获取已有条码
			var api_url = window.app.base_url + window.app.SellBarcodeInfoApi;
			var data2 = {};
			data2.username = window.app.username;
			data2.password = window.app.password;
			data2.sid = this.SID;
			data2.CtmType = window.app.CtmType;
			console.log(data2);
			var self2 = this;
			$.ajax({
				type : "POST",
				url : require.toUrl(api_url),
				data : {
					'data' : JSON.stringify(data2)
				},
				dataType : 'json',
				success : function(res) {
					console.log(res);
					res.forEach(function(e) {
						console.log(e.SInfoID);
						var row = self2.comp("FaHuoLieBiaoData").find([ 'SInfoID' ], [ e.SInfoID ]);
						row[0].val("ScanCount", row[0].val("ScanCount") + 1);

					});

					var scanFinish = true;
					self2.comp("FaHuoLieBiaoData").each(function(param) {
						if (param.row.val('ScanCount') !== param.row.val('PrdCount'))
							scanFinish = false;
					});
					if (scanFinish === true) {
						$(self2.getElementByXid("SendButton")).show();
					} else {
						$(self2.getElementByXid("SendButton")).hide();
					}
				},
				error : function(err) {
					return;
				}
			});
		}
	};

	Model.prototype.modelLoad = function(event) {
		console.log("modelLoad()");
		window.app.comp_FaHuoLieBiaoDataSell = this.comp("FaHuoLieBiaoData");
	};

	Model.prototype.SendButtonClick = function(event) {
		console.log("SendButtonClick()");
		var data = {};
		data.username = window.app.username;
		data.password = window.app.password;
		data.SID = this.SID;
		data.CtmType = window.app.CtmType;
		console.log(data);

		var api_url = window.app.base_url + window.app.SellSendApi;
		var self2 = this;
		$.ajax({
			type : "POST",
			url : require.toUrl(api_url),
			data : {
				'data' : JSON.stringify(data)
			},
			dataType : 'json',
			success : function(res) {
				console.log(res);
				if (res[0].msg === "发货成功") {
					self2.getParent().comp("ShippingRecord_Window").load("$UI/ls_shaomiao/Log.w");
					self2.getParent().comp("Main_Contents").to("ShippingRecord_Content");
					return;
				} else {
					justep.Util.hint("error");
					return;
				}
			},
			error : function(err) {
				justep.Util.hint("服务器出错，发货失败.");
				return;
			}
		});
	};

	return Model;
});